package com.edu.project;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import com.edu.project.activity.BaseActivity;
import com.edu.project.adapter.UserListAdapter;
import com.edu.project.bean.User;
import com.edu.project.bean.UserData;
import com.edu.project.mvp.presenter.UserListImpl;
import com.edu.project.mvp.presenter.UserPresenter;
import com.edu.project.mvp.view.UserView;
import com.edu.project.view.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheng on 2016/11/4.
 */
public class MainActivity extends BaseActivity implements UserView,PullToRefreshView.OnRefreshListener {


    @Bind(R.id.recyle)
    RecyclerView recyle;

    @Bind(R.id.refreshLayout)
    PullToRefreshView refreshLayout;

    private int mPageIndex = 1;
    private int mTotal;
    private int mCurrentNum = 0;

    private List<User> mUserList;
    private UserListAdapter mAdapter;
    private LinearLayoutManager mLayoutManger;

    private UserPresenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        init();
    }


    private void init() {
        mUserList = new ArrayList<>();
        mPresenter = new UserListImpl(this);
        mPresenter.loadUserData(mPageIndex,10);

        recyle.setHasFixedSize(true);
        mLayoutManger = new LinearLayoutManager(this);
        recyle.setLayoutManager(mLayoutManger);
        mAdapter = new UserListAdapter(this,mUserList);
        recyle.setAdapter(mAdapter);
        recyle.addOnScrollListener(mOnScrollListener);

        refreshLayout.setColorSchemeColors(Color.BLUE); // 颜色
        refreshLayout.setSmileStrokeWidth(6); // 设置绘制的笑脸的宽度
        refreshLayout.setSmileInterpolator(new LinearInterpolator()); // 笑脸动画转动的插值器
        refreshLayout.setSmileAnimationDuration(3000); // 设置笑脸旋转动画的时长
        //设置下拉刷新监听
        refreshLayout.setOnRefreshListener(this);
    }


    @Override
    protected int loadContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onRefresh() {
        mPageIndex = 1;
        if(mUserList != null){
            mUserList.clear();
        }
        mPresenter.loadUserData(mPageIndex,10);
    }

    public RecyclerView.OnScrollListener mOnScrollListener=new RecyclerView.OnScrollListener(){
        private int mLastVisibleItemPosition;
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mLastVisibleItemPosition = mLayoutManger.findLastVisibleItemPosition();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if(newState == RecyclerView.SCROLL_STATE_IDLE && mLastVisibleItemPosition+1==mAdapter.getItemCount()&&mAdapter.isShowFooter()){
                if (mPageIndex != 1 && mCurrentNum < mTotal) {
                    mPresenter.loadUserData(mPageIndex,10);
                } else {
                    //Toast("列表已全部加载完成");
                    return;
                }
            }
        }
    };

    @Override
    public void showProgress() {
    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void addData(UserData data) {
        mUserList.addAll(data.getList());
        mTotal = data.getTotal();
        mCurrentNum = mUserList.size();
        if (mCurrentNum >= mTotal) {
            mAdapter.isShowFooter(false);
            Toast("列表已全部加载完成");
        } else {
            mAdapter.isShowFooter(true);
            mPageIndex += 1;
        }
        mAdapter.notifyDataSetChanged();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void showLoadFail(String error) {
        mAdapter.isShowFooter(false);
        mAdapter.notifyDataSetChanged();
        Toast("请求数据出错");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
